import { storageLocal } from "/@/utils/storage";
import NotifMessage from "/@/utils/message/notification";
import { useRouter } from "vue-router";
import { download } from "/@/utils/download";

const router = useRouter();

export const FileTypes = {
  doc: "application/msword",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  xls: "application/vnd.ms-excel",
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  pdf: "application/pdf",
  png: "image/png",
  jpg: "image/jpeg",
  jpeg: "image/jpeg"
};

export const ImgFileTypes = {
  png: "image/png",
  jpg: "image/jpeg",
  jpeg: "image/jpeg"
};

export const acceptTypes = Object.values(FileTypes).join(",");

// banner 图类型
export const acceptImgTypes = Object.values(ImgFileTypes).join(",");

export const uploadUrl =
  import.meta.env.VITE_BASE_URL + "/rdms-admin/common/file/upload";

export const uploadComUrl =
  import.meta.env.VITE_BASE_URL + "/rdms-admin/common/file/upload";

export const uploadHeaders = () => {
  return { Authorization: storageLocal.getItem("rdms-info")?.accessToken };
};

export const handleError = err => {
  let myError = err.toString(); //转字符串
  myError = myError.replace("Error: ", ""); // 去掉前面的" Error: "
  myError = JSON.parse(myError); //转对象
  myError?.message && NotifMessage.error(myError?.message);

  if (myError?.body?.code && [401, 403].includes(myError?.body?.code)) {
    myError?.body?.message && NotifMessage.error(myError?.body?.message);
    router.push("/login");
    storageLocal.removeItem("rdms-info");
  }
};

export const beforeAvatarUpload = file => {
  const type = file.name.substring(file.name.lastIndexOf(".") + 1);
  if (!Object.keys(FileTypes).includes(type)) {
    NotifMessage.warning(`请上传${Object.keys(FileTypes).join("、")}等格式!`);
    return false;
  }

  const isLt2M = file.size / 1024 / 1024 < 10;

  if (!isLt2M) {
    NotifMessage.warning(`文件大小不能超过10M!`);
    return false;
  }
};

// banner 图验证
export const beforeImgUpload = file => {
  const type = file.name.substring(file.name.lastIndexOf(".") + 1);
  if (!Object.keys(ImgFileTypes).includes(type)) {
    NotifMessage.warning(
      `请上传${Object.keys(ImgFileTypes).join("、")}等格式!`
    );
    return false;
  }

  const isLt2M = file.size / 1024 / 1024 < 5;

  if (!isLt2M) {
    NotifMessage.warning(`文件大小不能超过5M!`);
    return false;
  }
};

export const handlePreview = file => {
  console.log(file, "file");
  if (file?.response?.code !== 200 || !file?.response?.data) return;
  download(file?.response?.data);
};

export const handleDownload = file => {
  download(file);
};

export const onFilterFileData = (list: any[]) => {
  if (!list || !list.length) return;
  const newFileData = list.map(item => {
    // if (item.status === "success") {
    return item.response.data;
    // }
  });
  return newFileData;
};

export const onInitFileList = (list: any[]) => {
  if (!list || !list.length || !list[0]?.fileName) return;
  const newFileList = list.map(item => {
    return {
      name: item?.fileName,
      response: {
        data: item,
        code: 200
      }
    };
  });
  return newFileList;
};
